package com.zhizuobiao.class3.resolver;

import com.zhizuobiao.class3.exception.MyException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by chengweixin on 2018/3/23.
 */

public class MyExceptionResolver implements HandlerExceptionResolver {
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                         Object o, Exception e) {

        String message = "";
        if (e instanceof NullPointerException) {
            message = "空指针";
        } else if (e instanceof MyException) {
            message = "职坐标 自定义异常";
        } else {
            message = e.getMessage();
        }

        ModelAndView mv = new ModelAndView();
        mv.setViewName("error");
        mv.addObject("message", message);

        return mv;
    }
}
